package cn.lhjl.day05.practice;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CalculatePhoneCharge {
    public static void main(String[] args) throws ParseException {
        //传两个满足格式yyyy年MM月dd日 HH时mm分ss秒的字符串，一个字符串表示的是接通时间，一个表示挂断时间。
        String str1 = "2019年11月21日 08时31分30秒";
        String str2 = "2019年11月22日 23时30分30秒";
        calculate(str1,str2);

    }
    public static void calculate(String str1,String str2) throws ParseException {
        //定义一个SimpleDateFormat类，格式为yyyy年MM月dd日 HH时mm分ss秒，用它的成员方法parse把字符串转为Date类对象
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        Date date1 = simpleDateFormat1.parse(str1);
        Date date2 = simpleDateFormat1.parse(str2);
        long day = 1000 * 60 * 60 * 24;
        long min = 1000 * 60;
        long starTime = (date1.getTime() + (8 * 1000 * 60 * 60)) % day;
        long endTime = (date2.getTime() + (8 * 1000 * 60 * 60)) % day;
        long ordinaryTime = 1000 * 60 * 60 * 8;
        long cheapTime = 1000 * 60 * 60 * 20;
        //把两个日期都转化为时间戳并求以毫秒为单位的差
        long time = date2.getTime() - date1.getTime();
        long leftTime = time % day;
        double charge = 0.0;
        if (starTime >= 0.0 && starTime < ordinaryTime) {
            if (leftTime <= ordinaryTime - starTime) {
                charge = (endTime - starTime) / min * 0.1;
            } else if (leftTime <= ordinaryTime - starTime + day / 2.0 && leftTime > ordinaryTime - starTime) {
                charge = (ordinaryTime - starTime) / min * 0.1 + (endTime - ordinaryTime) / min * 0.2;
            } else {
                if (endTime >= cheapTime) {
                    charge = (ordinaryTime - starTime) / min * 0.1 + 60 * 12 * 0.2 + (endTime - cheapTime) / min * 0.1;
                } else
                    charge = (ordinaryTime - starTime) / min * 0.1 + 60 * 12 * 0.2 + (endTime + day - cheapTime) / min * 0.1;
            }

        } else if (starTime >= ordinaryTime && starTime < cheapTime) {
            if (leftTime <= cheapTime - starTime) {
                charge = (endTime - starTime) / min * 0.2;
            } else if (leftTime <= cheapTime - starTime + day / 2.0 && leftTime > cheapTime - starTime) {
                if (endTime > cheapTime) {
                    charge = (cheapTime - starTime) / min * 0.2 + (endTime - cheapTime) / min * 0.1;
                } else charge = (cheapTime - starTime) / min * 0.2 + (endTime + day - cheapTime) / min * 0.1;
            } else
                charge = (cheapTime - starTime) / min * 0.2 + 60 * 12 * 0.1 + (endTime - ordinaryTime) / min * 0.2;

        } else {
            if (starTime >= cheapTime) {
                if (leftTime <= (day - starTime + ordinaryTime)) charge = leftTime / min * 0.1;
                else if (leftTime <= (day - starTime + ordinaryTime) + day / 2 && leftTime > day - starTime + ordinaryTime)
                    charge =
                            (day - starTime + ordinaryTime) / min * 0.1 + (endTime - ordinaryTime) / min * 0.2;
                else
                    charge = (day - starTime + ordinaryTime) / min * 0.1 + 12 * 60 * 0.2 + (endTime - cheapTime) / min * 0.1;

            } else{
                if (leftTime<=ordinaryTime-starTime) charge=leftTime/min*0.1;
                else if (leftTime>ordinaryTime-starTime&&leftTime<=ordinaryTime-starTime+day/2) charge=leftTime/min*0.1+
                        (endTime-ordinaryTime)/min*0.2;
                else  charge=leftTime/min*0.1+12*60*0.2+(endTime+day-cheapTime)/min*0.1;
            }
        }
        charge += (int)(time / day) * 12 * 60 * 0.3;
        System.out.println("费用：" + charge);
    }
}
